package com.fulihui.information.service;

import com.fulihui.information.api.ContentCommentsService;
import com.fulihui.information.biz.content.ContentCommentsBizManager;
import com.fulihui.information.core.db.CommentsOnOffDB;
import com.fulihui.information.core.repository.ContentCommentsRepository;
import com.fulihui.information.dal.dataobj.InfoContentComments;
import com.fulihui.information.dto.InfoContentCommentsDTO;
import com.fulihui.information.request.*;
import com.fulihui.information.result.ContentCommentsResponse;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.near.servicesupport.result.ResultBuilder.*;

/**
 * Created by LM on 2017/4/9.
 */
@Service(version = "1.0.0")
public class ContentCommentsServiceImpl implements ContentCommentsService {

    @Autowired
    private ContentCommentsBizManager commentsBizManager;
    @Autowired
    private ContentCommentsRepository commentsRepository;
    @Autowired
    private CommentsOnOffDB           commentsOnOffDB;

    @Override
    public TSingleResult<String> createComments(ContentCommentsCreateRequest request) {
        String commentsId = commentsBizManager.createComments(request);
        if (StringUtil.isNotBlank(commentsId)) {
            return succTSingle(commentsId);
        } else {
            return failTSingle(Errors.Commons.SYSTEM_ERROR);
        }
    }

    @Override
    public BaseResult updateComments(ContentCommentsUpdateRequest request) {
        InfoContentComments comments = new InfoContentComments();
        comments.setId(request.getCommentsId());
        comments.setStatus(request.getStatus());
        comments.setOperator(request.getOperator());
        boolean result = commentsRepository.update(comments);
        return result ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    @Override
    public BaseResult praiseTotalAdd(ContentCommentsPraiseRequest request) {
        commentsBizManager.praiseTotal(request.getCommentsId(), request.getUserId(),
            request.getContentId());
        return succ();
    }

    @Override
    public BaseResult replyTotalAdd(String commentsId) {
        commentsBizManager.replyTotal(commentsId);
        return succ();
    }

    @Override
    public TPageResult<InfoContentCommentsDTO> queryPage(ContentCommentsQueryRequest request) {
        // 查询结果集
        List<InfoContentCommentsDTO> values = commentsBizManager.queryPage(request);
        // 结果集为空则默认总数为零
        int count = (values != null && values.size() > 0) ? (int) commentsBizManager
            .queryPageCount(request) : 0;
        return ResultBuilder.succTPage(values, request.getPage(), request.getRows(), count);
    }

    @Override
    public ContentCommentsResponse<InfoContentCommentsDTO> queryPageExt(ContentCommentsQueryRequest request) {
        // 查询评论显示开关
        boolean onOff = commentsOnOffDB.onOff();
        // 查询结果集
        ContentCommentsResponse<InfoContentCommentsDTO> response = commentsBizManager.queryPageExt(
            request, onOff);
        // 结果集为空则默认总数为零
        int count = 0;
        /*(response.getValues() != null && response.getValues().size() > 0) ? (int) commentsBizManager
        .queryPageCountExt(request, onOff) : 0;*/
        response.setPage(request.getPage());
        response.setRows(request.getRows());
        int totalPage = count % request.getRows() == 0 ? count / request.getRows() : count
                                                                                     / request
                                                                                         .getRows()
                                                                                     + 1;
        response.setTotalPage(totalPage > 0 ? totalPage : 1);
        response.setTotalRows(count);
        response.setErrcode(0);
        response.setErrmsg("ok");
        return response;
    }

    @Override
    public BaseResult removeComments(ContentCommentsRequest request) {
        boolean result = commentsBizManager.removeContentComments(request);
        return result ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    @Override
    public TSingleResult<InfoContentCommentsDTO> queryByPK(ContentCommentsRequest request) {
        InfoContentCommentsDTO contentCommentsDTO = commentsBizManager.queryByPK(request);
        return succTSingle(contentCommentsDTO);
    }

    @Override
    public BaseResult openOnOff() {
        commentsOnOffDB.openOnOff();
        return succ();
    }

    @Override
    public BaseResult closeOnOff() {
        commentsOnOffDB.closeOnOff();
        return succ();
    }

    @Override
    public TSingleResult<Boolean> onOff() {
        boolean result = commentsOnOffDB.onOff();
        return succTSingle(result);
    }

    @Override
    public TPageResult<InfoContentCommentsDTO> queryPageUserComments(ContentCommentsQueryRequest request) {
        List<InfoContentCommentsDTO> values = commentsBizManager.queryPageUserComments(request);
        return succTPage(values, request.getPage(), request.getRows(), 0);
    }

}
